<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  	<title>创建FTP帐号</title>
  	<link rel="stylesheet" href="<%=path %>/css/public.css" type="text/css"  />
	<link rel="stylesheet" href="<%=path %>/css/module.css" type="text/css"  />
	<link rel="stylesheet" href="<%=path %>/css/skin_blue.css" type="text/css"  />
	<script type="text/javascript" src="<%=path %>/js/jquery/jquery-1.8.0.min.js"></script>
	<script type="text/javascript" src="<%=path %>/js/json.js"></script>
	<script type="text/javascript" src="<%=path %>/js/util.js"></script>
	<script type="text/javascript" src="<%=path %>/js/DatePicker/WdatePicker.js"></script>
	<script type="text/javascript">
		function notice(obj){
			if(obj.checked && obj.value=='2'){
				getObjById('receiver').style.display='';
			}else{
				getObjById('receiver').style.display='none';
			}
		}
		function GenePass(){
			$.ajax({
				type:'post',
				url:'<%=path %>/ftp!genePass.action',
				async:false,
				success:function(responseData){
					getObjById('password').value=responseData;
				}
			});
		}
		function checkUser(){
			var ftpUserName=getObjById('name').value;
			if(!ftpUserName || ftpUserName.length<1){
				return;
			}
			var param={
				ftpUserName:ftpUserName
			};
			$.ajax({
				type:'post',
				url:'<%=path %>/ftp!checkUser.action',
				data:'requestData='+Ext.util.JSON.encode(param),
				async:false,
				success:function(responseData){
					if(responseData=='1'){
						getObjById('name').focus();
						getObjById('checkuser').value='1';
						alert('该帐号名称已经存在，请重新输入！');
					}else{
						getObjById('checkuser').value='0';
					}
				}
			});
		}
		function utype(obj){
			getObjById('receiver').style.display='none';
			var objs=getObjsByName('noticetype');
			for(var i=0;i<objs.length;i++){
				objs[i].disabled=false;
				objs[i].checked=false;
			}
			if(obj.checked && obj.value=='3'){
				getObjById('tempdate').style.display='';
				for(var i=0;i<objs.length;i++){
					objs[i].disabled=true;
					if(objs[i].value=='2'){
						objs[i].checked=true;
						getObjById('receiver').style.display='';
					}
				}
			}else{
				getObjById('tempdate').style.display='none';
			}
		}
		function save(){
			var username=getObjById('name').value;
			var password=getObjById('password').value;
			if(username.length<1 ||password.length<1){
				alert('加 * 为必填项，该项值不能为空！');
				return ;
			}
			if(getObjById('checkuser').value=='1'){
				alert('该帐号名称已经存在，请重新输入！');
				return ;
			}
			var usertype=getRadioGroupValue('usertype');
			if(usertype=='-1'){
				alert('请选择帐号类型！');
				return ;
			}
			var tempDate='';
			if(usertype=='3'){//临时帐号
				if(getObjById('tempdate').value=='选择截止日期' || getObjById('tempdate').value.length<1){
					alert('请选择截止日期！');
					return ;
				}
				tempDate=getObjById('tempdate').value;
			}
			var noticetype=getRadioGroupValue('noticetype');
			if(noticetype=='-1'){
				alert('请选择邮件通知类型！');
				return ;
			}
			var mailReceiver='';
			if(noticetype=='2'){
				if(getObjById('receiver').value.length<1){
					alert('请填写指定收件人邮箱！');
					return ;
				}
				if(!checkMail(getObjById('receiver').value)){
					alert('指定收件人邮箱格式错误！');
					return ;
				}
				mailReceiver=getObjById('receiver').value;
			}
			var param={
				username:username,
				password:password,
				usertype:usertype,
				noticetype:noticetype,
				tempDate:tempDate,
				mailReceiver:mailReceiver
			};
			$.ajax({
				type:'post',
				url:'<%=path %>/ftp!ajaxSaveFtpUser.action',
				data:'requestData='+Ext.util.JSON.encode(param),
				async:false,
				success:function(responseData){
					if(responseData=='-1'){
						alert('帐号创建失败，请联系管理员！');
					}else{
						if(responseData=='-2'){
							alert('邮件发送不成功，请将用户名和密码手工发送（可能是邮件系统出现问题或网络出现故障，请及时通知数据技术部）！');
						}else{
							alert('帐号创建成功！');
							getObjById('frmFtpUserMaintain').submit();
						}
					}
				}
			});
		}
		function getRadioGroupValue(objid){
			var value='-1';
			var userObjs=getObjsByName(objid);
			for(var i=0;i<userObjs.length;i++){
				if(userObjs[i].checked){
					return userObjs[i].value;
				}
			}
			return value;
		}
		function showCalendar(id, format) {
			var obj=Ext.util.JSON.decode(getObjById('responseData').value);
			if(format.toLowerCase()=="yyyy-mm-dd" || format.toLowerCase()=="y-mm-dd") {
				WdatePicker({el:id,skin:'whyGreen',lang:'zh-cn',minDate:obj.minDate});
			} 
			return false;
		}
		function init(){
			var userID="<s:property value="#session.userID"/>";
			if(userID=="dpd"){
				getObjById('temptype').style.display="none";
				getObjById('tempname').style.display="none";
			}
		}			
	</script>
  </head>
  <body onload="init();">
  	<input type="hidden" id="responseData" value="<s:property value="responseData"/>">
  	<input type="hidden" id="checkuser" value="0">
  	<form action="<%=path %>/ftp!saveFtpUser.action" id="frmFtpUserMaintain" enctype="multipart/form-data" method="post">
  		<input type="hidden" id="requestData" name="requestData">
  	</form>
	<div class="tab">
	  <table width="50%"  border="0" class="formlist" cellpadding="0" cellspacing="0">
	    <thead>
	    	<tr>
	        	<th colspan="2"><span>FTP帐号信息</span></th>
	        </tr>
	    </thead>
	    <tfoot>
	      <tr>
	        <td colspan="2"><div class="bz">"<span class="red">*</span>"为必填项！</div>
	          <div class="btn">
	            <button name="btnSave" onclick="save();">创建帐号</button>
	          </div>
	        </td>
	      </tr>
	    </tfoot>
	    <tbody>
	      <tr>
	        <th width="20%"><span class="red">*</span>帐号</th>
	        <td width="80%"><input type="text"  style="width:200px" id="name" onblur="checkUser();"/></td>
	      </tr>
	      <tr>
	        <th><span class="red">*</span>密码</th>
	        <td>
	        	<input  style="width:200px"  id="password"/>
		        &nbsp;&nbsp;<button name="btnGenePass" onclick="GenePass();">生成密码</button>
	        </td>
	      </tr>
	      <tr>
	        <th><span class="red">*</span>帐号类型</th>
	        <td>
		        <input type="radio" name="usertype" value="1" onclick="utype(this);" checked="true"/>常规帐号<br/>
		        <input type="radio" name="usertype" value="2" onclick="utype(this);"/>长期帐号<br/>
		        <input id="temptype" type="radio" name="usertype" value="3" onclick="utype(this);"/><span id="tempname">临时帐号</span>&nbsp;&nbsp;&nbsp;
		        <input style="display:none" id="tempdate" value="选择截止日期" onclick="return showCalendar('tempdate','yyyy-MM-dd');"/>
	        </td>
	      </tr>
	      <tr>
	        <th><span class="red">*</span>邮件通知类型</th>
	        <td><input type="radio" name="noticetype" onclick="notice(this);" value="1" />系统默认收件人<br/><input type="radio" name="noticetype" onclick="notice(this);" value="2"/>指定收件人&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input style="display:none" type="text" style="width:200px"  id="receiver"/></td>
	      </tr>
	    </tbody>
	  </table>
	</div>
  </body>
</html>
